package com.second.hand.trading.server.controller;

import com.second.hand.trading.server.enums.ErrorMsg;
import com.second.hand.trading.server.model.SalesmanModel;
import com.second.hand.trading.server.service.Operlog;
import com.second.hand.trading.server.service.SalesmanService;
import com.second.hand.trading.server.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;

@CrossOrigin
@RestController
@RequestMapping("salesman")
public class SalesmanController {

    @Autowired
    private SalesmanService salesmanService;


    /**
     * 注册账号
     * @param salesmanModel
     * @return
     */
    @Operlog(object = "用户",operModul = "用户登录-注册",operDesc = "注册新用户",operType = "注册")
    @PostMapping("sign-in")
    public ResultVo signIn(@RequestBody  SalesmanModel salesmanModel) {
        System.out.println(salesmanModel);
        salesmanModel.setSignInTime(new Timestamp(System.currentTimeMillis()));
        if (salesmanModel.getAvatar() == null || "".equals(salesmanModel.getAvatar())) {
            salesmanModel.setAvatar("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        }
        if (salesmanService.salesmanSignIn(salesmanModel)) {
            return ResultVo.success(salesmanModel);
        }
        else return ResultVo.fail(ErrorMsg.REGISTER_ERROR);
    }

    /**
     * 登录，不安全，可伪造id，后期改进
     *
     * @param accountNumber
     * @param salesmanPassword
     * @param response
     * @return
     */
    @RequestMapping("login")
    @Operlog(object = "用户",operModul = "用户登录-注册",operDesc = "用户登录",operType = "登录")
    public ResultVo login(@RequestParam("accountNumber") @NotEmpty @NotNull String accountNumber,
                          @RequestParam("salesmanPassword") @NotEmpty @NotNull String salesmanPassword,
                          HttpServletResponse response) {
        SalesmanModel salesmanModel = salesmanService.salesmanLogin(accountNumber, salesmanPassword);
        System.out.println("登录：" + salesmanModel);
        if (null == salesmanModel) {
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }
        if(salesmanModel.getSalesmanStatus()!=null&&salesmanModel.getSalesmanStatus().equals((byte) 1)){
            return ResultVo.fail(ErrorMsg.ACCOUNT_Ban);
        }
        Cookie cookie = new Cookie("shSalesmanId", String.valueOf(salesmanModel.getId()));
//        cookie.setMaxAge(60 * 60 * 24 * 30);
        cookie.setPath("/");
        cookie.setHttpOnly(false);
        response.addCookie(cookie);
        return ResultVo.success(salesmanModel);
    }

    /**
     * 退出登录
     *
     * @param shSalesmanId
     * @param response
     * @return
     */
    @RequestMapping("logout")
    @Operlog(object = "用户",operModul = "用户主页",operDesc = "用户退出",operType = "退出")
    public ResultVo logout(@CookieValue("shSalesmanId")
                           @NotNull(message = "登录异常 请重新登录")
                           @NotEmpty(message = "登录异常 请重新登录") String shSalesmanId, HttpServletResponse response) {
        Cookie cookie = new Cookie("shSalesmanId", shSalesmanId);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return ResultVo.success();
    }

    /**
     * 获取用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("info")
    public ResultVo getOneUser(@CookieValue("shUserId") @NotNull(message = "登录异常 请重新登录")
                               @NotEmpty(message = "登录异常 请重新登录")
                                       String id) {
        return ResultVo.success(salesmanService.getSalesman(Long.valueOf(id)));
    }

    /**
     * 修改用户公开信息
     * @param id
     * @param salesmanModel
     * @return
     */
    @PostMapping("/info")
    public ResultVo updateUserPublicInfo(@CookieValue("shUserId") @NotNull(message = "登录异常 请重新登录")
                                     @NotEmpty(message = "登录异常 请重新登录")
                                             String id, @RequestBody  SalesmanModel salesmanModel) {
        salesmanModel.setId(Long.valueOf(id));
        if (salesmanService.updateSalesmanInfo(salesmanModel)) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.SYSTEM_ERROR);
    }


    /**
     * 修改密码
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @GetMapping("/password")
    @Operlog(object = "用户",operModul = "用户主页",operDesc = "修改密码",operType = "修改")
    public ResultVo updateUserPassword(@CookieValue("shUserId") @NotNull(message = "登录异常 请重新登录")
                                       @NotEmpty(message = "登录异常 请重新登录") String id,
                                       @RequestParam("oldPassword") @NotEmpty @NotNull String oldPassword,
                                       @RequestParam("newPassword") @NotEmpty @NotNull String newPassword) {
        if (salesmanService.updatePassword(newPassword,oldPassword,Long.valueOf(id))) {
            return ResultVo.success();
        }
        return ResultVo.fail(ErrorMsg.PASSWORD_RESET_ERROR);
    }
}
